package com.ljx.itbbs.server.article.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljx.itbbs.server.article.domain.entity.CollectRecordEntity;
import com.ljx.itbbs.server.article.domain.vo.req.ArticleCollectReq;
import com.ljx.itbbs.server.article.mapper.CollectRecordMapper;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CollectRecordDao extends ServiceImpl<CollectRecordMapper, CollectRecordEntity> {

    public CollectRecordEntity collectRecordDao(String objKey, Long userId) {
        return lambdaQuery().eq(CollectRecordEntity::getCollectObj, objKey)
                .eq(CollectRecordEntity::getUserId, userId)
                .one();
    }

    public List<CollectRecordEntity> getCollectRecordListByUId(Long uid) {
        return lambdaQuery().eq(CollectRecordEntity::getUserId, uid).list();
    }

    public IPage<CollectRecordEntity> getCollectRecordList(ArticleCollectReq req, Long uid) {
        IPage<CollectRecordEntity> page = new Page<>(req.getCurrPage(), req.getPageSize());
        LambdaQueryWrapper<CollectRecordEntity> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(CollectRecordEntity::getUserId, uid);
        return this.baseMapper.selectPage(page, lambdaQueryWrapper);
    }

    public void deleteCollectRecord(Long userId, String collectObj) {
        this.baseMapper.delete(new LambdaQueryWrapper<CollectRecordEntity>()
                .eq(CollectRecordEntity::getUserId, userId)
                .eq(CollectRecordEntity::getCollectObj, collectObj));
    }
}
